package cn.edu.scujcc.helloworld;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.gson.Gson;

import org.json.JSONObject;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import cn.edu.scujcc.helloworld.api.Api;
import cn.edu.scujcc.helloworld.api.ApiConfig;
import cn.edu.scujcc.helloworld.api.TtitCallback;
import cn.edu.scujcc.helloworld.entity.LoginResponse;
import cn.edu.scujcc.helloworld.util.AppConfig;
import cn.edu.scujcc.helloworld.util.StringUtils;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class LoginActivity extends BaseActivity {

    private EditText etAccount;
    private EditText etPwd;
    private Button btnLogin;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        etAccount = findViewById(R.id.et_account);
        etPwd = findViewById(R.id.et_pwd);
        btnLogin = findViewById(R.id.btn_login);


        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String account = etAccount.getText().toString().trim();
                String pwd = etPwd.getText().toString().trim();
                login(account,pwd);
            }
        });
    }
    private void login(String account,String pwd) {
        if (StringUtils.isEmpty(account)) {//判断文本框里面是否为空
            Toast.makeText(this, "请输入账号", Toast.LENGTH_SHORT).show();
            showToast("请输入账号");//提示用户输入账号
            return;
        }
        if (StringUtils.isEmpty(pwd)) {
            Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
            showToast("请输入密码");
            return;
        }
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("mobile", account);
        params.put("password", pwd);
        Api.config(ApiConfig.LOGIN, params).postRequest(new TtitCallback() {
            @Override
            public void onSuccess(final String res) {
               Log.e("onSuccess",res);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                      //  showToast(res);
                        Gson gson = new Gson();
                        LoginResponse loginResponse = gson.fromJson(res,LoginResponse.class);
                        if(loginResponse.getCode()==0){
                            String token = loginResponse.getToken();
                            SharedPreferences sp = getSharedPreferences("sp_ttit",MODE_PRIVATE);
                            SharedPreferences.Editor editor = sp.edit();
                            editor.putString("token",token);
                            editor.commit();
                            showToast("登录成功");
                            navigateTo(HomeActivity.class);
                        }else{
                            showToast("登录失败");
                        }
                    }
                });
            }

            @Override
            public void onFailure(Exception e) {

            }
        });

    }
}

   // private void login(String account,String pwd){
     //   if(StringUtils.isEmpty(account)){
          //  Toast.makeText(this, "请输入账号", Toast.LENGTH_SHORT).show();
      //      showToast( "请输入账号");
        //   return;
     //   }
     //   if(StringUtils.isEmpty(pwd)){
            //Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
        //    showToast("请输入密码");
       //     return;
      //  }


       // 创建OKHttpClient
      //  OkHttpClient client = new OkHttpClient.Builder()
       //         .build();
       // Map m = new HashMap();
       // m.put("modile","account");
       // m.put("password","pwd");
       // JSONObject jsonObject = new JSONObject(m);
       // String jsonStr = jsonObject.toString();
      //  RequestBody requestBodyJson =
            //    RequestBody.create(MediaType.parse("application/json;charset=utf-8")
              //  ,jsonStr);
        //创建Rquest
      //  Request request = new  Request.Builder()
            //    .url(AppConfig.BASE_URL + "/app/login")
            //    .addHeader("contentType","application/json;charset=UTF-8")
             //   .post(requestBodyJson)
              //  .build();
        //创建 call回调对象
       // final Call call = client.newCall(request);
        //发起请求
       // call.enqueue(new Callback(){
           // @Override
            //请求失败回调
           // public void onFailure(Call call, IOException e) {
           //     Log.e("onFailure",e.getMessage());
          //  }

           // @Override
            //请求成功回调
           // public void onResponse(Call call, Response response) throws IOException {
              //  final String result = response.body().string();
              //  runOnUiThread(new Runnable() {
                //    @Override
                 //   public void run() {
                   //     showToast(result); }});
            //}
       // });
   // }
//}